package com.xiaoleilu.hutool.test;

import java.io.File;

import org.dromara.hutool.core.text.StrUtil;
import org.junit.jupiter.api.Test;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

public class ReplacePublicStaticMethodsTests2 {
	
	@Test
	void test() {
		// 源代码文件路径
        String sourceFilePath = "E:\\code\\xls-web\\hutool-migrate-3.6\\hutool-core\\src\\main\\java\\com\\xiaoleilu\\hutool\\util\\ArrayUtil.java";
        // 目标类的完整路径，例如 "org.dromara.hutool.core.codec.hash.HashUtil"
//        StrUtil.subPre(sourceFilePath, 0)
        
        String targetClassPath = "org.dromara.hutool.core.array.ArrayUtil";
        
        
        try {
            // 解析源文件
            JavaParser javaParser = new JavaParser();
            ParseResult<CompilationUnit> parseResult = javaParser.parse(new File(sourceFilePath));
            CompilationUnit compilationUnit = parseResult.getResult().orElseThrow(() -> new RuntimeException("Parsing failed"));

            // 替换 public static 方法
            new ReplacePublicStaticMethodsVisitor(targetClassPath).visit(compilationUnit, null);

            // 输出修改后的代码
            System.out.println(compilationUnit.toString());

            // 可选：将修改后的代码写回文件
            // Files.write(Paths.get(sourceFilePath), compilationUnit.toString().getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	static class ReplacePublicStaticMethodsVisitor extends VoidVisitorAdapter<Void> {

        private final String targetClassPath;

        public ReplacePublicStaticMethodsVisitor(String targetClassPath) {
            this.targetClassPath = targetClassPath;
        }

        @Override
        public void visit(MethodDeclaration method, Void arg) {
            // 检查是否是 public static 方法
            if (method.isPublic() && method.isStatic()) {
                // 替换方法实现
                replaceMethodImplementation(method);
            }else {
            	System.err.println("发现非public/static方法: "+method);
            }
            super.visit(method, arg);
        }

        private void replaceMethodImplementation(MethodDeclaration method) {
            // 创建新的方法体
            BlockStmt newBody = new BlockStmt();

            // 创建方法调用表达式
	        MethodCallExpr methodCallExpr = new MethodCallExpr();
	        methodCallExpr.setScope(new NameExpr(targetClassPath));
	        methodCallExpr.setName(method.getNameAsString());
	        methodCallExpr.getArguments().addAll(method.getParameters().stream()
	                .map(param -> new NameExpr(param.getNameAsString()))
	                .toList());

            
            // 创建返回语句
            ReturnStmt returnStmt = new ReturnStmt(methodCallExpr);
            returnStmt.setExpression(methodCallExpr);

            // 设置新的方法体
            newBody.addStatement(returnStmt);
            method.setBody(newBody);
        }
    }
	
}
